<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/2
 * Time: 16:17
 */

namespace app\index\controller;

use app\index\controller\WeiXinPayController as WeiXinPay;
use app\index\model\Order;
use app\index\model\WxSet;

class PayController extends BaseController
{

    public function unified($openid, $out_trade_no, $body, $total_fee, $spbill_create_ip)
    {
        $notify_url = 'https://'.request()->server('HTTP_HOST').url('pay/notify_url');
        $ret = WxSet::get(1);
        $appid = $ret['appID'];        //准备数据（都在后台系统中可查到）
        $mch_id = $ret['mch_id'];      //商户号
        $key = $ret['key'];
        $weixinpay = new WeiXinPay($appid, $openid, $mch_id, $key, $out_trade_no, $body, $total_fee, $notify_url, $spbill_create_ip);
        $return = $weixinpay->pay();
        return $return;
    }

    public function notify_url()
    {
        $raw_xml = file_get_contents("php://input");
        libxml_disable_entity_loader(true);
        $post_data = json_decode(json_encode(simplexml_load_string($raw_xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if ($post_data['return_code'] == 'SUCCESS'){
            if ($post_data['result_code'] == 'SUCCESS'){
                //订单号
                $out_trade_no = $post_data['out_trade_no'] ? $post_data['out_trade_no'] : 0;
                //查询订单信息
                $order_info = Order::where('order_no',$out_trade_no)->find();
                //查询平台信息
                $platform_info = WxSet::get(1);
                //平台支付key
                $wxpay_key = $platform_info['key'];
                //接收到的签名
                $post_sign = $post_data['sign'];
                unset($post_data['sign']);
                //重新生成签名
                $newSign = $this->MakeSign($post_data, $wxpay_key);
                //签名统一，则更新数据库
                $str = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
                if ($post_sign == $newSign){
                    $myFile = fopen("../public/newfile.txt", "a+") or die("Unable to open file!");
                    $txt = '支付成功'.'==='
                        .$post_data['out_trade_no'].'==='
                        .$post_data['time_end'].'==='
                        .date('Y-m-d H:i:s',time())."\n";
                    fwrite($myFile, $txt);
                    fclose($myFile);
                    if ($order_info['pay_status'] && $order_info['pay_status'] == 1){
                        if ($order_info['table_id']){
                            $order = Order::update(['pay_status'=>2,'order_status'=>1,'pay_time'=>strtotime($post_data['time_end'])],['order_no'=>$out_trade_no]);
                            $ret = $this->waiter($order_info['table_id'],$out_trade_no);
                        }else{
                            $order = Order::update(['pay_status'=>2,'pay_time'=>strtotime($post_data['time_end'])],['order_no'=>$out_trade_no]);
                        }
                        if ($order){
                            $ret = $this->distribution($out_trade_no);
                            if ($ret == 1){
                                echo $str;
                            }elseif($ret == 2){
                                echo $str;
                            }
                        }
                    }else{
                        echo $str;
                    }
                }else{
                    $myFile = fopen("../public/newfile.txt", "a+") or die("Unable to open file!");
                    $txt = '验签失败'.'==='
                        .$post_data['out_trade_no'].'==='
                        .date('Y-m-d H:i:s',time())."\n";
                    fwrite($myFile, $txt);
                    fclose($myFile);
                }
            }else{
                $myFile = fopen("../public/newfile.txt", "a+") or die("Unable to open file!");
                $txt = $post_data['err_code'].'==='
                    .$post_data['err_code_des'].'==='
                    .date('Y-m-d H:i:s',time())."\n";
                fwrite($myFile, $txt);
                fclose($myFile);
            }
        }else{
            $myFile = fopen("../public/newfile.txt", "a+") or die("Unable to open file!");
            $txt = $post_data['return_msg'].'==='
                .date('Y-m-d H:i:s',time())."\n";
            fwrite($myFile, $txt);
            fclose($myFile);
        }
    }






}